package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    /**
     * 分页查询学院列表
     *
     * @param studentQueryParam
     * @return
     */
    @Override
    public PageResult<Student> selectStudent(StudentQueryParam studentQueryParam) {
        //设计分页参数
        PageHelper.startPage(studentQueryParam.getPage() , studentQueryParam.getPageSize());
        //调用查询所有的方法
        List<Student> studentList = studentMapper.selectStudent(studentQueryParam);
        //把调查所有数据的集合强壮为page对象
        Page p = (Page) studentList;

        PageResult<Student> pageResult = new PageResult<>();
        pageResult.setTotal(p.getTotal());
        pageResult.setRows(p.getResult());
        return pageResult;
    }

    /**
     * 删除学员
     *
     * @param ids
     */
    @Override
    public void deleteStudent(List<Integer> ids) {
        //删除学员信息
        studentMapper.deleteStudent(ids);
    }

    /**
     * 添加学员
     *
     * @param student
     */
    @Override
    public void postStudent(Student student) {
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.postStudent(student);
    }

    /**
     * 修改员工信息
     * 根据id回显信息
     *
     * @param id
     * @return
     */
    @Override
    public Student getStudentById(Integer id) {
        Student student = studentMapper.getStudentById(id);
        return student;
    }

    /**
     * 修改信息
     *
     * @param student
     */
    @Override
    public void putStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.putStudent(student);

    }
}
